﻿#region License
/*Copyright (c) 2010 Andrew Collard (andrewcollard@hotmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#endregion

using System;
using System.IO;

namespace TorchView.Game
{
  /// <summary>
  /// An effect applied to something
  /// </summary>
  public sealed class Effect
  {
    public string Name { get; set; }
    public string Something1 { get; set; }
    public string ParticleFX { get; set; }
    public string Something2 { get; set; }    
    public uint Unknown1 { get; set; }
    public uint Index { get; set; }  //Determines the type of bonus. I.e., 'OFTHETURTLE ARMOR BONUS' With Index = 60 corresponds to '% chance to reflect % of missile damage'.
    public DamageSourceType DamageType { get; set; }  //Varies occasionally, seems to be a level modifier for the effect, or a sub-modifier.
    public uint Unknown2 { get; set; }
    public uint Unknown3 { get; set; }
    public uint Unknown4 { get; set; }
    public byte Unknown5 { get; set; }
    public uint Unknown6 { get; set; }
    public uint Level { get; set; } //Level the effect was acquired. Same as item level on base effects. Seems to get boosted through heirlooming up to +5.
    public ulong Unknown7 { get; set; }
    public int Unknown8 { get; set; }
    public int Unknown9 { get; set; }
    public uint Unknown10 { get; set; }
    public int Duration { get; set; }
    public uint Unknown12 { get; set; }
    public float Unknown13 { get; set; }
    public float Bonus { get; set; }  //Sum of the Bonus for a given Name & Index1, rounded up, is the total + for the Affix shown in the item tooltip.
    public float RangeMin { get; set; }  //Range Min & Max come froom the Attrib .ADM files. The values get wacky after heirlooming. I haven't figured out the pattern.
    public float RangeMax { get; set; }  //Zero for Knockback. Bonus = RangeMin in that case
    public float PctMin { get; set; }  //May only be used for % of missile damage in the '% chance to reflect % of missile damage'
    public float PctMax { get; set; }  //May only be used for % of missile damage in the '% chance to reflect % of missile damage'


    public static Effect Read(BinaryReader reader)
    {       
      Effect effect = new Effect();
      effect.Name = SVTHelper.ReadString(reader);
      effect.Something1 = SVTHelper.ReadString(reader);
      effect.ParticleFX = SVTHelper.ReadString(reader);
      effect.Something2 = SVTHelper.ReadString(reader);
      effect.Unknown1 = reader.ReadByte();
      effect.Index = reader.ReadUInt32();
      effect.DamageType = (DamageSourceType) reader.ReadUInt32();

      effect.Unknown2 = reader.ReadByte(); //0x00
      effect.Unknown3 = reader.ReadByte(); //0x01
      effect.Unknown4 = reader.ReadByte(); //0x01
      effect.Unknown5 = reader.ReadByte(); //0x01
      effect.Unknown6 = reader.ReadUInt32();
      effect.Level = reader.ReadUInt32(); //Const 0x07000000 = 7
      effect.Unknown7 = reader.ReadUInt64();
      effect.Unknown8 = reader.ReadInt32(); //Const 0x64000000 = 100
      effect.Unknown9 = reader.ReadInt32(); //Const 0x64000000 = 100
      effect.Unknown10 = reader.ReadUInt32();
      effect.Duration = reader.ReadInt32(); //Constant 0x18fcffff = -1000. 4 on grand potions.
      effect.Unknown12 = reader.ReadUInt32();
      effect.Unknown13= reader.ReadSingle(); //Constant 0x0000803f = 1.0E+0
      effect.Bonus = reader.ReadSingle();
      effect.RangeMin = reader.ReadSingle();
      effect.RangeMax = reader.ReadSingle();
      effect.PctMin = reader.ReadSingle();
      effect.PctMax = reader.ReadSingle();

      return effect;
    }

    //private static string PetFormat(string Name)
    //{
    //    System.Globalization.CultureInfo cultureinfo = System.Threading.Thread.CurrentThread.CurrentCulture;
    //    System.Globalization.TextInfo textinfo = cultureinfo.TextInfo;

    //    if (Name.StartsWith(MagicWord.PET_TRANSFORM_TAG))
    //        return textinfo.ToTitleCase(Name.Substring(MagicWord.PET_TRANSFORM_TAG.Length).ToLower());
    //    else
    //        return textinfo.ToTitleCase(Name.ToLower());
    //}

    //public override string ToString()
    //{
    //    return string.Format(MagicWord.EFFECT_TYPE[Index],   //Get the actual effect text description.
    //        Bonus,                                              //{0} Plug in the effect bonus value.
    //        (uint)Math.Ceiling(PctMin),                        //{1} Plug in an alternate number ({0}%Chance to reflect % of missile damage)
    //        MagicWord.DAMAGE_SOURCE_TYPE[(int)DamageType],         //{2} Plug in damage type (Increases All {2} Damage by {0}%)
    //        Name,                       //{3} Plug in the skill name (Increase {3} Skill)
    //        (int)Duration,                                     //{4} Duration when a skill puts an effect on a target (Target Flees for {4} seconds)
    //        PetFormat(Name));                      //{5} Monster name (Transform into a {5} ...)
    //}
  }
}
